package sm.upgrade;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sm.upgrade.api.UpgradePhase;
import sm.util.SMMap;

public class SlamodulecontrolUpgrade implements UpgradePhase {

	private static Logger logger = LoggerFactory.getLogger(UpgradePhase.class);

	private HibernateTemplate template;

	public void setTemplate(HibernateTemplate template) {
		this.template = template;
	}

	@Override
	public void upgrade() {
		logger.info("Updating slamodulecontrol records...");
		List<Object> slamodulecontrols = template
				.find("from slamodulecontrol where table_name !=? and table_name != ?",
						new Object[] { "rootcausetask", "svcCartItem" });
		if (slamodulecontrols != null) {
			for (Object slamodulecontrol : slamodulecontrols) {
				SMMap slamodulecontrolMap = (SMMap) slamodulecontrol;
				List<String> ciFieldsSubscribe = new ArrayList<String>();
				ciFieldsSubscribe.add("affected.item");
				slamodulecontrolMap.put("ci_fields_subscribe",
						ciFieldsSubscribe);
				template.update("slamodulecontrol", slamodulecontrolMap);
			}
		}

		logger.info("  Finished updating slamodulecontrol records.");

	}

}
